package com.hlhj.carview.module

import android.content.Context
import com.example.mymvp.mvp.BaseModule
import com.hlhj.carview.Iview.LoginView
import com.hlhj.carview.bean.LoginBean
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.http.Apis
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.LoadingAction
import com.lzy.okgo.model.Response

/**
 * Created by Never Fear   on 2018\12\18 0018.
Never More....
 */
class LoginModule:BaseModule {
    var listener:LoginView?=null
    //发验证码
    fun sendSms(phone:String,c:Context){
        Apis?.sendSMS(phone)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c))
                            .subscribe(object :CustomAction<Response<MyBaseBean>>(c){
                                override fun httpSuccess(t: Response<MyBaseBean>) {
                                    listener?.sendSmsResp(t.body())
                                }
                            })
                }
    }
    //注册
    fun regis(phone:String,code:String,password:String,confirmPassword:String,c:Context){
        Apis.regis(phone, code, password, confirmPassword)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c))
                            .subscribe(object :CustomAction<Response<MyBaseBean>>(c){
                                override fun httpSuccess(t: Response<MyBaseBean>) {
                                    listener?.regisResp(t.body())
                                }
                            })
                }
    }
    //登录
    fun login(phone: String,password: String,c: Context){
        Apis?.login(phone, password)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c))
                            .subscribe(object :CustomAction<Response<LoginBean>>(c){
                                override fun httpSuccess(t: Response<LoginBean>) {
                                    listener?.loginResp(t.body())
                                }
                                override fun httpFailed(e: Throwable?) {
//                                    super.httpFailed(e)
                                    listener?.loginFailed(e?.message.toString())
                                }
                            })
                }
    }
    //修改密码
    fun changePwd(phone: String,code: String,password: String,confirmPassword:String,c:Context){
        Apis?.changePwd(phone, code, password, confirmPassword)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c))
                            .subscribe(object :CustomAction<Response<MyBaseBean>>(c){
                                override fun httpSuccess(t: Response<MyBaseBean>) {
                                    listener?.changePwdResp(t.body())
                                }
                            })
                }
    }
}